package com.tiantian.web.controller.auth;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tiantian.bean.PageBean;
import com.tiantian.domain.BackstageRole;
import com.tiantian.ret.Result;
import com.tiantian.service.BackstageRoleService;
import com.tiantian.util.PageUtil;
import com.tiantian.util.RoleUtils;
import com.tiantian.web.datatables.DataTableReturn;

@Controller
@RequestMapping("/auth/role")
public class BackstageRoleController {
	public static final Logger LOG = LoggerFactory.getLogger(BackstageRoleController.class);
	@Autowired
	private BackstageRoleService backstageRoleService;

	/**
	 * 后台管理角色列表
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "/index" })
	public String index(Model model) {
		return "auth/role";
	}

	/**
	 * 获取角色列表
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = { "/getRoleList" })
	public DataTableReturn getRoleList(HttpServletRequest request) {
		PageBean page = PageUtil.parseRequest2PageBean(request);
		Map<String, String> params = new HashMap<String, String>();
		List<BackstageRole> data = backstageRoleService.list(params, page);
		return new DataTableReturn(page.getDraw(), page.getTotal(), page.getTotal(), data);
	}

	/**
	 * 角色详情
	 * 
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = { "/roledetail" })
	public String roledetail(Model model, String id) {
		if (StringUtils.isNotBlank(id)) {
			BackstageRole role = backstageRoleService.get(id);
			model.addAttribute("role", role);
			model.addAttribute("auth", RoleUtils.parseAuthoritys(role.getAuthoritys(), role.getId()));
		}
		return "auth/roledetail";
	}

	/**
	 * 角色更新和添加
	 * 
	 * @param id
	 * @param name
	 * @param request
	 * @return
	 */
	@ResponseBody
	@SuppressWarnings("unchecked")
	@RequestMapping(value = { "/roleUpdate" })
	public Result<Object> roleUpdate(String id, String name, HttpServletRequest request) {
		try {
			if (StringUtils.isBlank(id)) {// 新增
				return backstageRoleService.saveRole(name, request.getParameterMap());
			} else {// 修改
				return backstageRoleService.updateRole(id, request.getParameterMap());
			}
		} catch (Exception e) {
			LOG.error("角色更新和添加异常", e);
			return new Result<Object>(false, "角色保存失败：" + e.getMessage());
		}
	}

	@ResponseBody
	@RequestMapping(value = { "/roleDel" })
	public Result<Object> roleDel(@RequestParam String id) {
		try {
			backstageRoleService.deleteByKey(id);
			return new Result<>(true, "删除角色成功");
		} catch (Exception e) {
			LOG.error("角色更新和添加异常", e);
			return new Result<Object>(false, "角色保存失败：" + e.getMessage());
		}
	}
}
